package com.example.gradu.graduationproject.service.impl;

import com.example.gradu.graduationproject.domain.Comment;
import com.example.gradu.graduationproject.mapper.CommentMapper;
import com.example.gradu.graduationproject.service.CommentService;
import com.example.gradu.graduationproject.util.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Comment)表服务实现类
 *
 * @author makejava
 * @since 2025-02-13 22:49:11
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param commentid 主键
     * @return 实例对象
     */
    @Override
    public R queryById(Integer commentid) {
        return R.ok().setData(this.commentMapper.queryById(commentid));
    }

    /**
     * 全查询
     *
     * @param comment 筛选条件
     * @return 查询结果
     */
    @Override
    public R queryAll(Comment comment) {
        List<Comment> comments = this.commentMapper.queryAll(comment);
        List<Comment> commentTree = buildCommentTree(comments);
//        反转评论列表顺序
        Collections.reverse(commentTree);
        return R.ok().setData(commentTree);
    }

//
    List<Comment> buildCommentTree(List<Comment> comments) {
        // 创建一个 Map，将 commentid 作为键
        Map<Integer, Comment> commentMap = comments.stream()
                .collect(Collectors.toMap(comment -> comment.getCommentid(), comment -> comment));

        List<Comment> rootComments = new ArrayList<>();

        for (Comment comment : comments) {
            if (comment.getParentid() == -1) {
                // 根评论
                rootComments.add(comment);
            } else {
                // 子评论
                Comment parent = commentMap.get(comment.getParentid());
                if (parent != null) {
                    parent.getChildren().add(comment);
                }
            }
        }

        return rootComments;
    }

    /**
     * 新增数据
     *
     * @param comment 实例对象
     * @return 实例对象
     */
    @Override
    public R insert(Comment comment) {
        this.commentMapper.insert(comment);
        return R.ok().setData(comment);
    }

    /**
     * 修改数据
     *
     * @param comment 实例对象
     * @return 实例对象
     */
    @Override
    public R update(Comment comment) {
        this.commentMapper.update(comment);
        return R.ok().setData(this.queryById(comment.getCommentid()));
    }

    /**
     * 通过主键删除数据
     *
     * @param commentid 主键
     * @return 是否成功
     */
    @Override
    public R deleteById(Integer commentid) {
        boolean del = this.commentMapper.deleteById(commentid) > 0;
        return R.ok().setData(del);
    }

    /**
     * 计数
     * @param comment 被计数评论的条件
     * @return 个数
     */
    @Override
    public R count(Comment comment){
        return R.ok().setData(this.commentMapper.count(comment));
    }
}

